/*
 * Copyright (c) 2006, 2022, webrx.cn All rights reserved.
 *
 */
package cn.annotation;

import java.lang.reflect.Field;
import java.lang.reflect.Method;

/**
 * <p>Project: jse2204 - AnnDemo
 * <p>Powered by webrx On 2022-10-10 09:15:01
 *
 * @author webrx [webrx@126.com]
 * @version 1.0
 * @since 17
 */

public class AnnDemo {
    public static void main(String[] args) throws Exception {
        //正射
        Lisi li = new Lisi();

        //反射
        //Class<Lisi> c1 = Lisi.class;
        //Class<?> c2 = Lisi.class;
        //Class c3 = li.getClass();
        //Class<?> c4 = Class.forName("cn.annotation.Lisi");
        //
        //System.out.println(c1);
        //System.out.println(c2);
        //System.out.println(c2 == c1);
        //System.out.println(c3 == c2);
        //System.out.println(c4);
        //System.out.println(c4 == c3);

        //利用反射机制，获取注解信息
        Class<?> c = Lisi.class;
        User ua = c.getAnnotation(User.class);
        System.out.println(ua.name());
        System.out.println(ua.age());
        System.out.println(ua.sex());

        //方法上的注解
        Method m = c.getMethod("show");
        User mu = m.getAnnotation(User.class);
        System.out.println(mu.name());
        System.out.println(mu.age());
        System.out.println(mu.sex());

        //属性上的注解
        Field name = c.getField("name"); //读取非private
        System.out.println(name.getAnnotation(Name.class).value());
        Field sname = c.getDeclaredField("sname");//可以读取private属性
        System.out.println(sname.getAnnotation(Name.class).value());


    }
}
